﻿using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NewUI.Enum;
using NewUI.ViewModels;

namespace NewUI.Models;

public partial class PlcList : ViewModelBase
{
    [ObservableProperty]private string? _id;
    [ObservableProperty]private string? _name;
    [ObservableProperty]private string? _cpuType;
    [ObservableProperty]private string? _ipAddress;
    [ObservableProperty]private int _rack;
    [ObservableProperty]private int _slot;
    [ObservableProperty]private int _timeout;
    [ObservableProperty]private string? _status;
    [ObservableProperty]private string? _textColor = "Success";
    
    [ObservableProperty]private bool _isConnected;
    [ObservableProperty] private bool _isConnectLoading;

    public PlcListViewModel? plcListViewModel;

    public PlcList()
    {
        _cpuType = PlcType.S71500.ToString();
        _timeout = (int)TimeOutOption.TimeOutOption_2000;
    }
    public PlcList(string name, string plcType, string ipAddress, int rack, int slot)
    {
        _name = name;
        _cpuType = plcType;
        _ipAddress = ipAddress;
        _rack = rack;
        _slot = slot;
    }

    public PlcList(string id, string name, string cpuType, string ipAddress, int rack, int slot, int timeout, string status, bool isConnected, bool isConnectLoading)
    {
        _id = id;
        _name = name;
        _cpuType = cpuType;
        _ipAddress = ipAddress;
        _rack = rack;
        _slot = slot;
        _timeout = timeout;
        _status = status;
        _isConnected = isConnected;
        _isConnectLoading = isConnectLoading;
    }

    [RelayCommand]
    public async Task SelectPlcListItem(PlcList plcList)
    {
        plcListViewModel.CurrentPlcList = plcList;
    }

    [RelayCommand]
    public async Task Connect(PlcList plcList)
    {
        if (plcListViewModel != null)
        {
            plcListViewModel.TestConnectPlcList(plcList);
        }
        
    }
}